import jsonpath
import requests
from api.global_conf import BASEURL, LOGPATH
from api.user import login
from utils.index import Logs
log = Logs(LOGPATH)


def findByPid(data):
    try:
        res = requests.post(BASEURL + "/shop/mfindByPid", data=data)
    except Exception as result:
        log.error(f'接口访问失败: /shop/mfindByPid \n报错信息: {result}')
    else:
        log.info(f'接口访问成功: /shop/mfindByPid \n访问数据: {res.text}')
        return res


def addCart(data, headers):
    try:
        res = requests.post(BASEURL + "/shop/maddCart", data=data, headers=headers)
    except Exception as result:
        log.error(f'接口访问失败: /shop/maddCart \n报错信息: {result}')
    else:
        log.info(f'接口访问成功: /shop/maddCart \n访问数据: {res.text}')
        return res


def myCart(headers):
    try:
        res = requests.post(BASEURL + "/shop/mmyCart", headers=headers)
    except Exception as result:
        log.error(f'接口访问失败: /shop/mmyCart \n报错信息: {result}')
    else:
        log.info(f'接口访问成功: /shop/mmyCart \n访问数据: {res.text}')
        return res


def saveOrder(headers):
    try:
        res = requests.get(BASEURL + "/shop/msaveOrder", headers=headers)
    except Exception as result:
        log.error(f'接口访问失败: /shop/msaveOrder \n报错信息: {result}')
    else:
        log.info(f'接口访问成功: /shop/msaveOrder \n访问数据: {res.text}')
        return res


def payOrder(json, headers):
    try:
        res = requests.post(BASEURL + "/shop/mpayOrder", json=json, headers=headers)
    except Exception as result:
        log.error(f'接口访问失败: /shop/mpayOrder \n报错信息: {result}')
    else:
        log.info(f'接口访问成功: /shop/mpayOrder \n访问数据: {res.text}')
        return res


login1 = login({
    "username": "zhangfeidameinv",
    "password": "123"
})
print(login1.json())
cookie = login1.headers['Set-Cookie']
print(cookie)
print(findByPid({
    "pid": 1
}).json())
add = addCart({
    "product.pid": 1,
    "count": 2
}, {
    "cookie": cookie
})
print(add.json())
my = myCart({
    "cookie": cookie
})
print(my.json())
sOrder = saveOrder({
    "cookie": cookie
})
print("=======================================\n",sOrder.json())
oid = sOrder.json()['data']['oid']
print(oid)
pay = payOrder({
    "addr": "北京",
    "name": "zhangsan",
    "oid": oid,
    "pd_FrpId": "BOCO-NET-B2C",
    "phone": "111111111"
}, {
    "cookie": cookie,
    "Content-Type": "application/json",
    "charset": "utf-8"
})
print(pay.json())
